﻿using JetBrains.Annotations;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Tree;

namespace GammaJul.ReSharper.GammaXaml.Psi.Xaml {

	/// <summary>
	/// Represents the initialization of a dependency property.
	/// </summary>
	public class DpInitInfo {
		private readonly DpRegistrationMethod _registrationMethod;
		private readonly ITypeofExpression _propertyTypeofExpression;

		public static readonly DpInitInfo UnknownInfo = new DpInitInfo(DpRegistrationMethod.Unknown, null);

		/// <summary>
		/// Gets the method used to register the dependency property.
		/// </summary>
		public DpRegistrationMethod RegistrationMethod {
			get { return _registrationMethod; }
		}

		/// <summary>
		/// Gets the typeof expression that correspond to the dependency property type registration, if applicable.
		/// </summary>
		[CanBeNull]
		public ITypeofExpression PropertyTypeofExpression {
			get { return _propertyTypeofExpression; }
		}

		/// <summary>
		/// Gets the registered type of the dependency property, if applicable.
		/// </summary>
		[CanBeNull]
		public IType RegisteredType {
			get { return _propertyTypeofExpression != null ? _propertyTypeofExpression.ArgumentType : null; }
		}

		/// <summary>
		/// Returns whether this <see cref="DpInitInfo"/> is valid.
		/// </summary>
		/// <returns><c>true</c> if the instance is valid, <c>false</c> otherwise.</returns>
		public bool IsValid() {
			return _propertyTypeofExpression == null || _propertyTypeofExpression.IsValid();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DpInitInfo"/> class.
		/// </summary>
		/// <param name="registrationMethod">The method used to register the dependency property.</param>
		/// <param name="propertyTypeofExpression">The typeof expression that correspond to the dependency property type registration, if applicable.</param>
		public DpInitInfo(DpRegistrationMethod registrationMethod, [CanBeNull] ITypeofExpression propertyTypeofExpression) {
			_registrationMethod = registrationMethod;
			_propertyTypeofExpression = propertyTypeofExpression;
		}
	}

}